﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace LevelEditor
{
    public enum WallType
    {
        NoneWall = 1,
        AllWall = 2,
        RightWall = 3,
        RightBotWall = 4,
        BotWall = 5,
        LeftBotWall = 6,
        LeftWall = 7,
        TopLeftWall = 8,
        TopWall = 9,
        TopRightWall = 10,
        TopBotWall = 11,
        TopLeftBotWall = 12,
        TopRightBotWall = 13,
        LeftRightWall = 14,
        LeftBotRightWall = 15,
        LeftTopRightWall = 16
    }

    public class ImageParser
    {
        private string _inputFileName;
        private string _outputFileName;
       
        private Bitmap _image;
        private int _width, _height;
        private Color _nullColor;

        private string _mainCharacter = "20", _checkpnt = "21", _item = "22", _obstacle = "23";
        private bool _isMainGenerated = false;

        public Color WallColor { private get; set;}
        public Color CkpntColor { private get; set; }
        public Color ItemColor { private get; set; }
        public Color MainColor { private get; set; }
        public Color ObstacleColor { private get; set; }
        public int CustomizedUnit { private get; set; }
        public ImageParser(string file, int size) 
        {
            _inputFileName = file;
            CustomizedUnit = size;
            _nullColor = Color.FromArgb(0, 255, 255, 255);
            _outputFileName = _inputFileName.Substring(0, _inputFileName.Length - 4) + ".txt";
        }

        public void ParseImage() 
        {
            _image = new Bitmap(_inputFileName);
            _height = _image.Height;
            _width = _image.Width;

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(_outputFileName))
            {
                for (int i = CustomizedUnit / 2; i < _height; i += CustomizedUnit)
                {
                    string line = "";
                    for (int j = CustomizedUnit / 2; j < _width; j += CustomizedUnit)
                    {
                        Color clr = _image.GetPixel(j, i);

                        if (IsSameColor(clr, WallColor))
                        {
                            line += " " + GetWallType(clr, j < CustomizedUnit ? _nullColor : _image.GetPixel(j - CustomizedUnit, i),
                                j > _width - CustomizedUnit ? _nullColor : _image.GetPixel(j + CustomizedUnit, i),
                                i < CustomizedUnit ? _nullColor : _image.GetPixel(j, i - CustomizedUnit),
                                i > _height - CustomizedUnit ? _nullColor : _image.GetPixel(j, i + CustomizedUnit));
                        }
                        else if (IsSameColor(clr, MainColor) && !_isMainGenerated) 
                        {
                            line += " " + _mainCharacter;
                            _isMainGenerated = true ;
                        }
                        else if (IsSameColor(clr, CkpntColor)) 
                        {
                            line += " " + _checkpnt;
                        }
                        else if (IsSameColor(clr, ItemColor))
                        {
                            line += " " + _item;
                        }
                        else if (IsSameColor(clr, ObstacleColor))
                        {
                            line += " " + _obstacle;
                        }
                        else if (IsSameColor(clr, Color.White))
                        {
                            line += " 0";
                        }
                    }
                    file.WriteLine(line);
                }
                file.Close();
            }
        }


        private bool IsSameColor(Color clr1, Color clr2) {
            if (clr1.R == clr2.R && clr1.G == clr2.G && clr1.B == clr2.B)
                return true;
            return false;
        } 

        private String GetWallType(Color thisColor, Color leftUnit, Color rightUnit, Color topUnit, Color botUnit)
        {
            WallType result = WallType.NoneWall;
            if (thisColor.Equals(leftUnit) && thisColor.Equals(rightUnit) && thisColor.Equals(topUnit) && thisColor.Equals(botUnit))
                result = WallType.NoneWall;
            else if (thisColor.Equals(leftUnit) && thisColor.Equals(rightUnit) && thisColor.Equals(botUnit) && !thisColor.Equals(topUnit))
                result = WallType.TopWall;
            else if (thisColor.Equals(leftUnit) && thisColor.Equals(rightUnit) && thisColor.Equals(topUnit) && !thisColor.Equals(botUnit))
                result = WallType.BotWall;
            else if (thisColor.Equals(topUnit) && thisColor.Equals(botUnit) && thisColor.Equals(leftUnit) && !thisColor.Equals(rightUnit))
                result = WallType.RightWall;
            else if (thisColor.Equals(topUnit) && thisColor.Equals(botUnit) && thisColor.Equals(rightUnit) && !thisColor.Equals(leftUnit))
                result = WallType.LeftWall;
            else if (thisColor.Equals(rightUnit) && thisColor.Equals(botUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(topUnit))
                result = WallType.TopLeftWall;
            else if (thisColor.Equals(leftUnit) && thisColor.Equals(botUnit) && !thisColor.Equals(rightUnit) && !thisColor.Equals(topUnit))
                result = WallType.TopRightWall;
            else if (thisColor.Equals(leftUnit) && thisColor.Equals(topUnit) && !thisColor.Equals(rightUnit) && !thisColor.Equals(botUnit))
                result = WallType.RightBotWall;
            else if (thisColor.Equals(rightUnit) && thisColor.Equals(topUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(botUnit))
                result = WallType.LeftBotWall;
            else if (thisColor.Equals(rightUnit) && !thisColor.Equals(topUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(botUnit))
                result = WallType.TopLeftBotWall;
            else if (thisColor.Equals(leftUnit) && !thisColor.Equals(topUnit) && !thisColor.Equals(rightUnit) && !thisColor.Equals(botUnit))
                result = WallType.TopRightBotWall;
            else if (thisColor.Equals(topUnit) && !thisColor.Equals(rightUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(botUnit))
                result = WallType.LeftBotRightWall;
            else if (thisColor.Equals(botUnit) && !thisColor.Equals(rightUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(topUnit))
                result = WallType.LeftTopRightWall;
            else if (thisColor.Equals(leftUnit) && thisColor.Equals(rightUnit) && !thisColor.Equals(topUnit) && !thisColor.Equals(botUnit))
                result = WallType.TopBotWall;
            else if (thisColor.Equals(topUnit) && thisColor.Equals(botUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(rightUnit))
                result = WallType.LeftRightWall;
            else if (!thisColor.Equals(topUnit) && !thisColor.Equals(botUnit) && !thisColor.Equals(leftUnit) && !thisColor.Equals(rightUnit))
                result = WallType.AllWall;
            return ((int)result).ToString();
        }
    }
}
